package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/admin/order")
@Api(tags = "订单管理")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 条件查询订单
     *
     * @param ordersPageQueryDTO 条件查询参数
     * @return 订单列表
     */
    @GetMapping("/conditionSearch")
    @ApiOperation(value = "条件查询订单")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageResult pageResult = ordersService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 统计订单
     *
     * @return 订单统计结果
     */
    @GetMapping("/statistics")
    @ApiOperation(value = "统计订单")
    public Result<OrderStatisticsVO> statistics() {
        OrderStatisticsVO orderStatisticsVO = ordersService.statistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 根据订单号查询订单
     *
     * @param orderId 订单号
     * @return 订单信息
     */
    @GetMapping("details/{orderId}")
    @ApiOperation(value = "查询订单详情")
    public Result<OrderVO> getOrderDetails(@PathVariable("orderId") Long orderId) {
        OrderVO orderVO = ordersService.getOrderDetail(orderId);
        return Result.success(orderVO);
    }

    /**
     * 接单
     *
     * @param ordersConfirmDTO 接单参数
     * @return 接单结果
     */
    @PutMapping("/confirm")
    @ApiOperation(value = "接单")
    public Result<String> confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        ordersService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 拒单
     *
     * @param ordersRejectionDTO 拒单参数
     * @return 拒单结果
     */
    @PutMapping("/rejection")
    @ApiOperation(value = "拒单")
    public Result<String> rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        ordersService.rejection(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 取消订单
     *
     * @param ordersCancelDTO 取消参数
     * @return 取消结果
     */
    @PutMapping("cancel")
    @ApiOperation("取消订单")
    public Result<String> cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        ordersService.cancelByAdmin(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 派送订单
     *
     * @param orderId 订单id
     * @return 派送结果
     */
    @PutMapping("/delivery/{orderId}")
    @ApiOperation("派送订单")
    public Result<String> delivery(@PathVariable("orderId") Long orderId) {
        ordersService.delivery(orderId);
        return Result.success();
    }

    /**
     * 完成订单
     *
     * @param orderId 订单id
     * @return 完成结果
     */
    @PutMapping("/complete/{orderId}")
    @ApiOperation("完成订单")
    public Result<String> complete(@PathVariable("orderId") Long orderId) {
        ordersService.complete(orderId);
        return Result.success();
    }
}
